<?php
	include('../inc/config.php');
	include(ROOT.'/html/_javascript_css.html.php');
	
	$producto_id = request('producto_id',0);
	
	$Meses = meses();

	
	$primero_de_mes        = mktime(0, 0, 0, date("m")  , 1, date("Y"));
	$mes_anterior  = mktime(0, 0, 0, date("m")-2, 1,   date("Y"));

	$accion = request('accion','');
	
	if($accion=='nuevo'){
	
		$data = $_POST['data'];

		//-------------- Cambio el formato de las fechas	
		list($dia,$mes,$ano) = explode('/',$data['desde']);
		$data['desde'] = mktime(0,0,0,$mes,$dia,$ano);
		list($dia,$mes,$ano) = explode('/',$data['hasta']);
		$data['hasta'] = mktime(0,0,0,$mes,$dia,$ano);
		
        $db->debug = false;
        $ok = $db->Replace('reservas', $data,'id', $autoquote = true); 

	
	} elseif($accion=='eliminar'){
		$reserva_id = request('reserva_id',0);
		$sql = "delete from reservas where id='$reserva_id'";
		$rs = $db->Execute($sql);
	}

	
	//------------------------------------------------------------------------------------
	//   Tengo Reservas para este alojamiento?
	//------------------------------------------------------------------------------------
	$sql = "select * from reservas where producto_id='$producto_id' order by desde ASC";
	$rs  = $db->Execute($sql);
	$ReservasOriginal = $rs->GetRows();
	$Reservas = array();
	$Ocupado = array();
	if($ReservasOriginal) {
		foreach($ReservasOriginal as $ro){
		
			$desde = $ro['desde'];
			$cantidad = $ro['cantidad_noches'];			
			if ($ro['tipo']==1){
				for($i=0;$i<=$cantidad;$i++){	
					$fecha = mktime(0,0,0,date("m",$desde),date("d",$desde)+$i,date("Y",$desde));
					$Reservas[$fecha] = $ro['nombre'];
				}
			} else {
				for($i=0;$i<=$cantidad;$i++){	
					$fecha = mktime(0,0,0,date("m",$desde),date("d",$desde)+$i,date("Y",$desde));
					$Ocupado[$fecha] = $ro['nombre'];
				}
			}
		}
	
	
	}
	
	
	
	$Rango = array();
	for($i=1;$i<=12;$i++){
		$new  = mktime(0, 0, 0, date("m",$mes_anterior)+$i, 1,   date("Y",$mes_anterior));
		
		$inicio = mktime(0, 0, 0, date("m",$new), 1,   date("Y",$new));
		$proximo  = mktime(0, 0, 0, date("m",$new)+1, 1,   date("Y",$new));
		
		$fin_de_mes  = mktime(0, 0, 0, date("m",$proximo), date("d",$proximo)-1,   date("Y",$proximo));
		
		$dias_del_mes = date("d",$fin_de_mes);
		
		$fila = 1;
		
		for($d=1;$d<=$dias_del_mes;$d++){
				
			$dia = mktime(0, 0, 0, date("m",$fin_de_mes), $d,   date("Y",$fin_de_mes));
			
			$w = iif( date("w",$dia)==0,7,date("w",$dia) ) * 1;

		
			$Rango[$new][$fila][$w] = $dia;
			
			$fila = iif($w==7,$fila+1,$fila);
		
		}
		
		
	}
	
	$hoy = mktime(0,0,0,date("m"),date("d"),date("Y"));
	
	
	echo "<table style='width:1000px'>
			<tr>
				<td style='width:400px;  vertical-align:top;'>
		";
?>
	<h4>Reservar</h4>
	<script type="text/javascript" src="<?php echo URL;?>/js/datepicker/date.js"></script>
	<script type="text/javascript" src="<?php echo URL;?>/js/datepicker/jquery.datePicker.js"></script>
	<link rel="stylesheet" href="<?php echo URL;?>/js/datepicker/datepicker.css" type="text/css" media="screen" />	

<form class="form-horizontal" id='frm_contacto' action='<?php echo URL;?>/account/mi_cuenta_ocupacion.html.php?producto_id=<?php echo $producto_id;?>' method='post'>
	<input type="text" id="inputNombre" class='input-xlarge' name='data[nombre]'  style='margin-bottom:10px;' value='' placeholder='Nombre y Apellido'>
	<div class='clearfix'></div>	
	<input type="email" id="inputEmail" class='input-xlarge'  name='data[email]'  style='margin-bottom:10px;' value='' placeholder='Email'>
	<div class='clearfix'></div>	
	<input type="text" id="inputTelefono" class='input-xlarge'  name='data[telefono]' placeholder="Telefono"  style='margin-bottom:10px;' value=''>
	<div class='clearfix'></div>
		
	<?php
		$un_dia = 60*60*24;
		$desde = date("d/m/Y",time()+($un_dia));
		$hasta = date("d/m/Y",(time()+($un_dia*2)));
		$cantidad_de_noches = ($hasta - $desde);
	?>
	<table style='font-size:11px;'>
		<tr>
			<td>
				<input type='text' name="data[desde]" id="start-date"  class="date-pick"  readonly placeholder='Desde el...' style='width:90px;' required value='<?php echo $desde;?>'/>
			</td>
			<td>
				<input type='text' name="data[hasta]" id="end-date"   class="date-pick" readonly placeholder='Hasta el...' style='width:90px;' required value='<?php echo $hasta;?>'/>
			</td>
			
		</tr>
		<tr>
			<td>Cant.Noches:</td>
			<td>
				<input type='text' name="data[cantidad_noches]" id="cantidad-noches" class="input-small" readonly placeholder='1' value='<?php echo $cantidad_de_noches;?>' />
			</td>
		</tr>
		<tr>
			<td>Tipo Reserva:</td>
			<td>
				<select name='data[tipo]'>
					<option value='2'>Ocupado</option>				
					<option value='1'>Reserva</option>
				</select>
			</td>
		</tr>
		
	</table>
	<div class='clearfix'></div>	
	<textarea id="frm_mensaje" name="data[notas]" placeholder='Notas extras' rows='5' class='input-xlarge'></textarea>
	<div class='clearfix'></div>			
	<input type='hidden' name='data[producto_id]' id='producto_id' value='<?php echo $producto_id;?>' />
	<input type='hidden' name='accion' id='accion' value='nuevo' />
	<button type="submit" class="btn btn-primary input-xlarge" >Guardar Cambios
		<img src='<?php echo URL;?>/img/loading.gif' id='img_enviado' style='display:none;float:right;margin-right:30px;'>
	</button>
	<div class='clearfix'></div>	
		
</form>


	<?php
	if($ReservasOriginal){
		echo "<table style='font-size:12px;width:90%;'>";
		foreach($ReservasOriginal as $ro){
			$color = iif($color=='#EEE','#FFF','#EEE');
			echo "<tr style='background:$color;height:30px;line-height:30px;'>";
			echo "<td style='padding-left:10px; width:12px;'><a href='".URL."/account/mi_cuenta_ocupacion.html.php?producto_id=$producto_id&reserva_id={$ro[id]}&accion=eliminar' title='Eliminar'><img src='".URL."/img/del.gif'></a></td>";
			echo "<td style='padding-left:10px; width:36px;'>".date("d/m",$ro['desde'])."</td>";
			echo "<td style='width:50px;'>al ".date("d/m",$ro['hasta'])."</td>";
			echo "<td style='padding-left:10px;'>{$ro['nombre']}</td>";
			echo "</tr>";
		}
		echo "</table>";	
	} ?>

	
		
<?php		
	echo "		</td>
				<td style='vertical-align:top;'>
		";
			
			echo "<table style='font-size:11px;margin-bottom:10px;margin-left:30px;'>
					<tr>
						<td class='hoy referencia'>&nbsp;</td><td style='padding-right:40px;'>Hoy</td>
						<td class='feriado referencia'>&nbsp;</td><td style='padding-right:40px;'>Feriado</td>
						<td class='ocupado referencia'>&nbsp;</td><td  style='padding-right:40px;'>Ocupado</td>
						<td class='reserva referencia'>&nbsp;</td><td>Reservado</td>
					</tr>
				</table>";
	
			echo "<table id='contenedora'>";
			$x =1;
			foreach($Rango as $clave=>$r){

				if($x==1){	echo "<tr>"; }
			
				echo "<td class='td-contenedora'>";
			
				$mes = $Meses[date("m",$clave)*1]."/".date("Y",$clave);
				echo "<table class='tbl-calendar'>";
				echo "<tr ><thead><td colspan='7'>$mes</td></thead></tr>";
				echo "<tr class='titulos-dias'>
						<td>LUN</td>
						<td>MAR</td>
						<td>MIE</td>
						<td>JUE</td>
						<td>VIE</td>
						<td>SAB</td>
						<td>DOM</td>
					</tr>";
				
				foreach($r as $fila=>$valor){
					echo "<tr>";			
					foreach($valor as $col=>$data){
						
						if($fila==1 and !isset($ya_paso_por_aca)){
							if($col==2){ echo "<td class='vacia'>&nbsp;</td>";}	
							if($col==3){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
							if($col==4){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
							if($col==5){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
							if($col==6){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
							if($col==7){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
							$ya_paso_por_aca = true;
						}
						
						$dia_de_hoy = iif($data==$hoy,'hoy','');
						$es_feriado = iif($data==$feriado,'feriado','');
						
						$reserva    = iif(isset($Reservas[$data]),'reserva','');
						$ocupado    = iif(isset($Ocupado[$data]),'ocupado','');
						
						$title='';
						if(!empty($reserva)){	$title = $Reservas[$data]; }
						if(!empty($ocupado)){	$title = $Ocupado[$data]; }
						
							

						echo "<td id='id-$data' class='$ocupado $dia_de_hoy $es_feriado $reserva' title='$title'>".date("d",$data)."</td>";	
					}
					if($col==6){ echo "<td class='vacia'>&nbsp;</td>";}	
					if($col==5){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==4){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==3){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==2){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==1){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	

					echo "</tr>"; // Termina una fila de la semana
					
				}
				
				unset($ya_paso_por_aca);
				
				echo "</table>";
				echo "<br><br>";

				
				echo "</td>";
				$x++;
				if($x==4){ echo "</tr>"; $x=1;}
			
			}
			
			echo "</tr>";
			
			echo "</table>";
		
		echo "</td>
			</tr>
		</table>";
	
?>

<style>
.hoy{
	border:2px solid #333 !important;
}

.tbl-calendar {
	border:1px solid #FEBA02;
	width:200px;
	font-size:12px;
}
thead{
	background:#FEBA02;
	text-align:center;
	color:#333;
	font-weight:bold;
	line-height:20px;
}
.titulos-dias{
	background:#F7E3AD;
	text-align:center;
	color:#333;
	font-weight:bold;
	line-height:20px;
}
.vacia {
	background:#F7F2E5;
}
.tbl-calendar td {
	border:1px solid #FEBA02;
	text-align:center;
	font-size:11px;
}

.td-contenedora{
	vertical-align:top;
	padding:0px 5px;

}

.referencia {
	width:20px;
}
.ocupado{
	background:#FF0000;
}
.reserva{
	background:#13A552;
}

.feriado{
	border:2px solid #1D3FB6 !important;
}

</style>

<script type="text/javascript">

	jQuery(function () { 
	
		
		$('#start-date').change(function() {
			var desde = $('#start-date').val();
			var hasta = $('#end-date').val();
			var one_day=1000*60*60*24; 
			var x=desde.split("/"); 
			var y=hasta.split("/"); 
			var date1=new Date(x[2],(x[1]-1),x[0]);  
			var date2=new Date(y[2],(y[1]-1),y[0]);
			
			if(date1>=date2){
				tiempo=date1.getTime()
				total=date2.setTime(tiempo+one_day);
				day  =date2.getDate();
				month=date2.getMonth()+1;
				year =date2.getFullYear();
				nuevo = day+"/"+month+"/"+year;
				$('#end-date').val(nuevo);
			}
			
			var month1=x[1]-1;
			var month2=y[1]-1;        			
			_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
			$("#cantidad-noches").val(_Diff);
			
			
			
			
		});

		$('#end-date').change(function() {
			var desde = $('#start-date').val();
			var hasta = $('#end-date').val();
			var one_day=1000*60*60*24; 
			var x=desde.split("/"); 
			var y=hasta.split("/"); 
			var date1=new Date(x[2],(x[1]-1),x[0]);  
			var date2=new Date(y[2],(y[1]-1),y[0]);
			
			if(date1>=date2){
				tiempo=date1.getTime()
				total=date2.setTime(tiempo+one_day);
				day  =date2.getDate();
				month=date2.getMonth()+1;
				year =date2.getFullYear();
				nuevo = day+"/"+month+"/"+year;
				$('#end-date').val(nuevo);
			}
			
			
			var month1=x[1]-1;
			var month2=y[1]-1;        			
			_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
			$("#cantidad-noches").val(_Diff);
		});

		
	});  

		$('.date-pick').datePicker({clickInput:true})
		
		$('#start-date').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#end-date').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		$('#end-date').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#start-date').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);

		
</script>
	